﻿using FinalPointSet.Model.Constant;
using FinalPointSet.View;
using FinalPointSet.View.Dialog;
using System.Threading;
using System.Windows;

namespace FinalPointSet
{
    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App : Application
    {
        private const string UniqueAppName = Config.APP_Name; // 确保这个名称是唯一的  
        private static Mutex? mutex;

        protected override void OnStartup(StartupEventArgs e)
        {

            mutex = new Mutex(true, UniqueAppName, out bool isOwned);

            if (!isOwned)
            {
                // 如果互斥体已经被另一个实例拥有，则显示消息并退出当前实例
                Current.Shutdown();
            }
            else
            {
                // 继续正常的启动过程  
                base.OnStartup(e);
            }
        }

        protected override void OnExit(ExitEventArgs e)
        {
            // 释放互斥体  
            if (mutex != null)
            {
                mutex.ReleaseMutex();
                mutex.Dispose();
            }
            //释放托盘
            StartView.notifyIcon?.Dispose();
            base.OnExit(e);
        }
    }
}
